package com.czy.seed.mvc.home.controller;

import com.alibaba.fastjson.JSONObject;
import com.czy.seed.mvc.home.entity.ExcelStatisticsVo;
import com.czy.seed.mvc.home.entity.Notice;
import com.czy.seed.mvc.home.service.NoticeService;
import com.czy.seed.mvc.sys.entity.SysUser;
import com.czy.seed.mvc.util.ExcelTest;
import com.czy.seed.mvc.util.ExportExcel;
import com.czy.seed.mvc.util.Res;
import com.czy.seed.mybatis.base.QueryParams;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author: liuyun
 * Created by liuyun  14:38 2017/11/27.
 */

@RestController
@RequestMapping("/home/notice")
public class NoticeController {


    public static Logger logger = LoggerFactory.getLogger(NoticeController.class);

    @Autowired
    private NoticeService noticeServic;


    //查询公告信息
    @RequestMapping("/select")
    public Res select() {
        Map<String, Object> pageInfo = new HashMap<String, Object>(16);
        try {
            QueryParams queryParams = new QueryParams(Notice.class);
            QueryParams.Criteria criteria = queryParams.createCriteria();
            queryParams.orderBy("id").desc();
            criteria.andLessThan("startTime", new Date());
            criteria.andGreaterThan("overTime", new Date());
            List<Notice> noticeList = noticeServic.selectListByParams(queryParams);
            pageInfo.put("noticelist", noticeList);
            logger.info("查询公告信息成功");
        } catch (Exception e) {
            logger.error("查询公告信息失败");
        }
        return Res.ok(pageInfo);
    }

    //删除公告信息
    @RequestMapping("/deleteNotice")
    public Res deleteNotice(HttpServletRequest request) {
        Map<String, Object> pageInfo = new HashMap<String, Object>(16);
        String noticeList = request.getParameter("notice");
        try {
            Notice notice = JSONObject.parseObject(noticeList, Notice.class);
            int i = noticeServic.deleteByPrimaryKey(notice.getId());
            pageInfo.put("notice", i);
            logger.info("删除公告信息成功:" + notice.getName());
        } catch (Exception e) {
            logger.error("删除失败");
        }
        return Res.ok(pageInfo);
    }

    //修改公告信息
    @RequestMapping("/updateNotice")
    public Res updateNotice(HttpServletRequest request) {
        Map<String, Object> pageInfo = new HashMap<String, Object>(16);
        String notice = request.getParameter("notice");
        try {
            Notice noticeunit = JSONObject.parseObject(notice, Notice.class);
            int i = noticeServic.updateByPrimaryKey(noticeunit);
            pageInfo.put("notice", i);
            logger.info("修改成功:" + noticeunit.getName());
        } catch (Exception e) {
            logger.error("修改失败");
        }
        return Res.ok(pageInfo);
    }

    //新增公告信息
    @RequestMapping("/addNotice")
    public Res addNotice(HttpServletRequest request) {
        Map<String, Object> pageInfo = new HashMap<String, Object>(16);
        String notice = request.getParameter("notice");
        try {
            Notice noticeunit = JSONObject.parseObject(notice, Notice.class);
            noticeunit.setNcLabel(noticeunit.getNcLabel());
            noticeunit.setName(noticeunit.getName());
            noticeunit.setStartTime(new Date());
            noticeunit.setOverTime(noticeunit.getOverTime());
            noticeunit.setDetails(noticeunit.getDetails());
            int i = noticeServic.insert(noticeunit);
            pageInfo.put("noticeunit", noticeunit);
            pageInfo.put("notice", i);
            logger.info("新增成功：" + noticeunit.getName());
        } catch (Exception e) {
            logger.error("新增失败");
        }
        return Res.ok(pageInfo);
    }

}
